;(function () {
  window.oVue = new Vue({
    el: '#companyFrameWork',
    data: {
      subId: 0,
      subData: [],
      data: [],
      oData: {},
      index: 0,
      showButtonFlag: false,
      name: '',
      show: false,
      count: 0,
      projectList: []
    },
    computed: {
      treeList () {
        let arr = [];
        let parent, children;
        parent = this.data
        children = this.data
        for (let i = parent.length-1; i >= 0; i--) {
          let item0 = parent[i];
          for (let j = 0; j < children.length; j++) {
            let item1 = children[j];
            if (item0.id == item1.parentId) {
              item0.lndex++
              item0.children.push(item1)
            }
          }
        }
        for (let k = 0; k < parent.length; k++) {
          if (Number(parent[k].parentId) > 0) {
            parent.splice(k, 1)
            k--
          }
        }
        arr = parent
        return arr
      },

    },
    filters: {},
    methods: {
      //新建第一级
      queryEditOrganization () {
        let type = JSON.parse(window.localStorage.getItem('userMes')).tenantType

        hlhttp.post({
          url: 'organization',
          data: {short_name: this.name, pid: 0, type: '#'},
          success: () => {
            hl.toast('新增组织成功')
            this.showButtonFlag = false
            this.show = false
            this.getdata()
          }
        })
      },
      cancelEditOrganization () {
        this.show = false
      },
      getdata (fn) {
        hlhttp.get({
          url: 'sub_organizations',
          data: {pid: this.subId},
          success: (data) => {

            setTimeout(() => {
              if (!$('.framework-tree-list').length) {
                this.showButtonFlag = true
              }
            }, 300)
            data.map(item => {
              item.parentId = this.subId;
              item.flag = true;
              item.oflag = false;
              item.show = false;
              item.children = [];
              this.subData.push(item)
            })

            this.subData = hlArrayUtil.removeRepeat(this.subData, 'id');
            this.subData = this.subData.sort(hlArrayUtil.compareAvg('id'))
            this.data = JSON.parse(JSON.stringify(this.subData))
            if (fn) fn();
          }
        })
      },
      getPidData (id, type) {
        if (id == this.subId) return
        this.subId = id;
        this.getdata()
        if (type == 'GS') {
          this.companyProject(id)
        }
        if (type != 'GS') {
          this.projectList = []
        }
      },
      //获取公司项目
      companyProject (id) {
        hlhttp.get({
          url: 'company/projects',
          data: {company_id: id},
          success: (data) => {
            this.projectList = data
          }
        })
      },
      //高度设置
      setHeight () {
        var h = $('.cm-main-content').outerHeight(true);
        if (document.querySelector('.left-tissue')) {
          document.querySelector('.left-tissue').style.height = (h+20) + 'px';
          document.querySelector('.right-tissue').style.height = (h+20) + 'px';
          document.querySelector('.right-tissue-overflow').style.height = (h-25) + 'px';
        }
      },
    },
    created () {
      this.setHeight()
    },
    mounted () {
      this.getdata()
      window.onresize = () => {
       this.setHeight()
      }
    }
  })
} ())
